##parameters=form=None, format='csv'
from Products.KnowledgeEngine.Utils.helpers import renderCSV 

if not form:
    form = context.REQUEST.form

setHeader = context.REQUEST.RESPONSE.setHeader

# Only return something if we have results
results = list(context.demo_search(form=form, returnRaw=False))
data = None
if len(results):
    # Assume the model we're CSV exporting is the first one as it makes
    # no sense to export to CSV for multiple model types
    model = results[0].getModel()

    # Regular CSV Export - only exports the searchIndexes listed in the mapping
    if format == 'csv':
        schema = context.demo_search_mapping()
        fields = [field['idx'] for field in schema]

        data = model.toCSV(assessments=results, searchIndices=fields, renderHeader=True, displayReportContent=False)

    elif format == 'fullcsv':
        # NOTE: Setting displayReportContent to True will enable the MultipleChoice
        #       and Selection questions to use the report content instead of the
        #       the component's value(s) e.g.:
        # 
        #       return model.toCSV(results, renderHeader=True, displayReportContent=True)
        data = model.toCSV(results, renderHeader=True, displayReportContent=False)

    # Finally, ensure the correct HTTP headers are set so that the data
    # is downloaded
    setHeader( 'content-type', 'text/csv' )
    setHeader( 'content-disposition', 'attachment;filename=search_results.csv' )

return data